Ext.define('App.commons.report.KpiGrid', {
    extend: 'Ext.grid.Panel',
    alias: ['widget.KpiGrid'],
    userCls: 'high-line35',
    width: '100%',
    tbar: {height: 0},
    scrollable: true,
    reserveScrollbar: false,// 预留滚动条位置
    disableSelection: false,
    rowLines: true,
    loadMask: true,
    sortableColumns: false,
    enableColumnMove: false,
    minHeight: 100,
    viewConfig: {
        getRowClass(record, index, rowParams, store) {

            if (record.get('迭代') == '基线') {
                return 'kpi_baseline';
            } else if (record.get('迭代') == '目标') {
                return 'kpi_target';
            }
            return null;
        }
    },
    constructor: function () {
        var config = arguments[0];
        var reportConfiguration = config.reportConfiguration;
        var targetConfiguration = config.targetConfiguration;
        var datas = config.reportDatas;
        var cloneDatas = [];
        Ext.Array.each(datas, function (item) {
            cloneDatas.push(Ext.clone(item));
        })
        if (datas.length > 0) {
            var target = Ext.clone(datas[0]);
            target['迭代'] = '目标';
            target.id = Ext.id();
            for (key in target) {
                if (!Ext.isEmpty(targetConfiguration[key])) {
                    target[key] = targetConfiguration[key].targetValue;
                }
            }
            cloneDatas.push(target);

            var baseline = Ext.clone(datas[0]);
            baseline['迭代'] = '基线';
            baseline.id = Ext.id();
            for (key in baseline) {
                if (!Ext.isEmpty(targetConfiguration[key])) {
                    baseline[key] = targetConfiguration[key].baseLineValue;
                }
            }
            cloneDatas.push(baseline);
        }

        this.forceFit = reportConfiguration.forceFit || false;
        var kpis = reportConfiguration.kpis;
        this.store = Ext.create('Ext.data.Store', {
            fields: this.createFields(kpis),
            data: cloneDatas
        });
        this.columns = this.createColumns(kpis, targetConfiguration);
        this.callParent(arguments);
    },
    createColumns: function (kpis, targetConfiguration) {
        var me = this;
        var columns = [];
        Ext.Array.each(kpis, function (kpi) {
            var dataType = kpi.kpiConfiguration.dataType;
            var xtype = 'gridcolumn';
            if ((dataType == 'float' || dataType == 'int')) {
                xtype = 'KpiColumn';
            } else if (dataType == 'date') {
                xtype = 'datecolumn';
            }
            var targetKpi = targetConfiguration[kpi.kpiConfiguration.field];
            var baselineValue = targetKpi ? targetKpi.baseLineValue : null;
            var targetValue = targetKpi ? targetKpi.targetValue : null;
            var maxValue = targetKpi ? targetKpi.maxValue : null;
            var minValue = targetKpi ? targetKpi.minValue : null;
            var column = {
                xtype: xtype,
                dataIndex: kpi.kpiConfiguration.field,
                text: kpi.kpiConfiguration.field,
                type: dataType,
                format: kpi.kpiConfiguration.format,
                judgmentStateMode: kpi.kpiConfiguration.judgmentStateMode,
                formatter: kpi.kpiConfiguration.formatter,
                width: kpi.kpiConfiguration.width || null,
                baselineValue: baselineValue,
                targetValue: targetValue,
                maxValue: maxValue,
                minValue: minValue,
                hidden: kpi.hidden,
                height: 35,
                renderer: function (value, metaData, record, rowIndex, colIndex, store, view) {
                    if (metaData.column.type == 'date') {
                        if (!isNaN(value)) {
                            value = Ext.Date.format(value, metaData.column.format || 'Y-m-d')
                        } else {
                            value = null;
                        }
                    } else if (metaData.column.type == 'int' || metaData.column.type == 'float') {
                        if (isNaN(value)) {
                            value = '-';
                        } else {
                            if (record.get('迭代') != '基线' && record.get('迭代') != '目标') {
                                var cc = metaData.column;

                                if (!Ext.isEmpty(cc.judgmentStateMode)) {
                                    var html = '<div style="height: 100%;width: 100%;" title="{0}"><img src="css/icons/{1}" style="margin-right:5px;"/>{2}</div>';
                                    var title = [];
                                    if (!Ext.isEmpty(cc.baselineValue)) {
                                        title.push("基线值：" + cc.baselineValue);
                                    }
                                    if (!Ext.isEmpty(cc.targetValue)) {
                                        title.push("目标值：" + cc.targetValue);
                                    }
                                    if (!Ext.isEmpty(cc.minValue)) {
                                        title.push("最小值：" + cc.minValue);
                                    }
                                    if (!Ext.isEmpty(cc.maxValue)) {
                                        title.push("最大值：" + cc.maxValue);
                                    }

                                    if (!Ext.isEmpty(cc.targetValue) && !Ext.isEmpty(cc.maxValue) && !Ext.isEmpty(cc.minValue)) {
                                        if (cc.judgmentStateMode == '上限优先') {
                                            if (value >= cc.targetValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-green.png', value);
                                            } else if (value >= cc.minValue && value < cc.targetValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-yellow.png', value);
                                            } else if (value < cc.minValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-red.png', value);
                                            }
                                        }
                                        if (cc.judgmentStateMode == '下限优先') {
                                            if (value <= cc.targetValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-green.png', value);
                                            } else if (value > cc.targetValue && value <= cc.maxValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-yellow.png', value);
                                            } else if (value > cc.maxValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-red.png', value);
                                            }
                                        }
                                        if (cc.judgmentStateMode == '均值优先') {
                                            if ((value >= (cc.minValue + (cc.targetValue - cc.minValue)) / 2) && (value <= (cc.targetValue + (cc.maxValue - cc.targetValue)) / 2)) {
                                                return Ext.String.format(html, title.join("，"), 'circular-green.png', value);
                                            } else if (value >= cc.minValue &&
                                                ((value < (cc.minValue + (cc.targetValue - cc.minValue) / 2) || value > (cc.targetValue + (cc.maxValue - cc.targetValue) / 2))) &&
                                                value <= cc.maxValue
                                            ) {
                                                return Ext.String.format(html, title.join("，"), 'circular-yellow.png', value);
                                            } else if (value > cc.maxValue || value < cc.minValue) {
                                                return Ext.String.format(html, title.join("，"), 'circular-red.png', value);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else if (Ext.isEmpty(value)) {
                        value = "-";
                    }
                    return value;
                }
            }
            columns.push(column)
        });
        return columns;
    },
    createFields: function (kpis) {
        var fields = [];
        Ext.Array.each(kpis, function (kpi) {
            fields.push(kpi.kpiConfiguration.field)
        });
        return fields;
    }
})